#!/usr/bin/perl -w

use strict;
use Getopt::Long qw(:config pass_through);
use FindBin;
use lib $FindBin::Bin;
use webkitdirs;

chdirWebKit();

my $os;
my $derivedSoucesSuffix;
my $msvs_version;
my $gyp_file;
my $gyp_dir;
my $enable_jit = -1;
my $enable_llint = -1;

my %project_file_suffixes = (
    2008 => '.vcproj',
    2010 => '.vcxproj',
);

my %options = (
    'os=s' => \$os,
    'msvs_version=s' => \$msvs_version,
    'jit!' => \$enable_jit,
    'llint!' => \$enable_llint,
    );

GetOptions(%options);

if ($os eq 'win') {
    $derivedSoucesSuffix = 'orbis';
    $msvs_version = '2010' unless $msvs_version;
    $gyp_dir = "Tools/manx/win";
    $enable_jit = 1 if $enable_jit == -1;
    $enable_llint = 1 if $enable_llint == -1;
} elsif ($os eq 'psp2') {
    $derivedSoucesSuffix = 'psp2';
    $msvs_version = '2010' unless $msvs_version;
    $gyp_dir = "Tools/manx/psp2_10";
    $enable_jit = 0 if $enable_jit == -1;
    $enable_llint = 0 if $enable_llint == -1 and $enable_jit == 1;
    $enable_llint = 1 if $enable_llint == -1 and $enable_jit == 0;
} elsif ($os eq 'orbis') {
    $derivedSoucesSuffix = 'orbis';
    $msvs_version = '2010' unless $msvs_version;
    $gyp_dir = "Tools/manx/orbis";
    $enable_jit = 1 if $enable_jit == -1;
    $enable_llint = 1 if $enable_llint == -1;
} else {
    die "Unknown OS: ${os}";
}

chdir $gyp_dir;
$gyp_file = 'manx.gyp';

system("python",  "../../../../tools/gyp/gyp",  "--depth", ".", '-G', "msvs_version=${msvs_version}", "-DOS=${os}", "-DENABLE_JIT=${enable_jit}", "-DENABLE_LLINT=${enable_llint}", $gyp_file) == 0 or die "Failed to run gyp";

chdirWebKit();

my @project_names = qw(
    WTF
    JavaScriptCore
    JscLLIntOffsetExtractor
    WebCore
    WebCoreDerived
    WebCoreDom
    WebCoreHtml
    WebCoreSvg
    WebCoreTestSupport
    WebKit
    WebKit2
    WebKitTestRunner
);

if ($os eq 'orbis') {
    push(@project_names, qw(WTF_detached JSCConsole JscJitCompiler_proxy JscJitCompiler_detached));
    push(@project_names, qw(WTF_CoreIPC WebCore_CoreIPC CoreIPC));
} elsif ($os eq 'psp2') {
    push(@project_names, qw(JSCConsole JscJitCompiler_proxy));
} elsif ($os eq 'win') {
    push(@project_names, qw(JSCJITProxy JSCConsole));
}

foreach (@project_names) {
    my $project_name = $_;
    my $proj_file = "${gyp_dir}/${project_name}${project_file_suffixes{$msvs_version}}";
    my $tmp_proj_file = "${proj_file}.bak";
    print "Modifying $proj_file\n";
    rename $proj_file, $tmp_proj_file;
    if ($msvs_version eq '2008') {
        open(INPUT, "-|", "python", "Tools/Scripts/pretty-vcproj-manx.py", $tmp_proj_file) or die;
    } else {
        open(INPUT, "-|", "python", "Tools/Scripts/pretty-vcxproj-manx.py", $tmp_proj_file) or die;
    }
    open(OUTPUT, ">", $proj_file) or die;
    foreach (<INPUT>) {
        s/&quot;SN_TARGET_PSP2&quot;/SN_TARGET_PSP2/g if ($os eq 'psp2');
        s/\$\(OutDir\)\\?DerivedSources/..\\..\\..\\DerivedSources_${derivedSoucesSuffix}/g;
        print(OUTPUT $_);
    }
    close INPUT;
    close OUTPUT;
    #unlink($tmp_proj_file);

    if ($msvs_version eq '2010') {
        my $file = "${gyp_dir}/${project_name}${project_file_suffixes{$msvs_version}}.filters";
        my $tmp_file = "${file}.bak";
        print "Modifying $file\n";
        rename $file, $tmp_file;
        open(INPUT, "-|", "python", "Tools/Scripts/pretty-vcxproj-manx.py", $tmp_file) or die;
        open(OUTPUT, ">", $file) or die;
        while (<INPUT>) {
            s/\$\(OutDir\)\\?DerivedSources/..\\..\\..\\DerivedSources_${derivedSoucesSuffix}/g;
            print(OUTPUT $_);
        }
        close INPUT;
        close OUTPUT;
        #unlink($tmp_file);
    }
}

